package com.jiepos.mpos.biz.user.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.user.service.AppRoleInfService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.AppMenuAuth;
import com.jiepos.mpos.common.entity.App_Role_InfEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;

/**
 * 角色管理控制层
 * @ClassName: AppRoleInfController
 * @Description: 角色管理控制层
 * @author 孙永
 * @date 2017年9月19日 下午3:03:34
 *
 */
@Controller
@RequestMapping("roleinf")
public class AppRoleInfController extends BaseController{
	
	@Autowired
	private AppRoleInfService appRoleInfService;
	
	/**
	 * 角色管理页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getRoleInfList.do")
	public ModelAndView getRoleInfList(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView("/module/user/role/roleinf_list");
		model.addObject("insLevel", request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_LEVEL));
		return super.getDetail(request, response, model);
	}

	/**
	 * 获取列表数据
	 * @param request
	 * @param response
	 * @param pagination
	 * @param condition 查询条件
	 * @return
	 */
	@RequestMapping("/getRoleInfListJson.do")
	@ResponseBody
	public Map<String, Object> getRoleInfListJson(HttpServletRequest request, HttpServletResponse response, Pagination pagination, App_Role_InfEntity condition){
		return appRoleInfService.getRoleInfPageList(request, pagination, condition);
	}
	
	/**
	 * 详情页面
	 * @param request
	 * @param response
	 * @param uuid
	 * @return
	 */
	@RequestMapping("/getRoleInfDetail.do")
	public ModelAndView getRoleInfDetail(HttpServletRequest request, HttpServletResponse response, String uuid){
		ModelAndView model = new ModelAndView("/module/user/role/roleinf_detail");
		model.addObject("entity", appRoleInfService.getRoleInfByUuid(uuid, request));
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 保存角色信息
	 * @param request
	 * @param response
	 * @param entity
	 * @return
	 */
	@RequestMapping("/saveRoleInf.do")
	public ModelAndView saveRoleInf(HttpServletRequest request, HttpServletResponse response, App_Role_InfEntity entity) {
		appRoleInfService.saveRoleInf(entity, super.getLoginUser(request, response));
		Map map=super.getLoginUser(request, response);	
		return new ModelAndView(new RedirectView("getRoleInfList.do"), map);
	}
	
	/**
	 * 删除角色信息
	 * @param request
	 * @param response
	 * @param role_uuids 角色标识
	 * @return
	 */
	@RequestMapping("/deleteRoleByIds.do")
	public ModelAndView deleteMenuByIds(HttpServletRequest request, HttpServletResponse response, String role_uuids) {
		appRoleInfService.deleteMenuByIds(role_uuids);
		Map map = super.getLoginUser(request, response);
		return new ModelAndView(new RedirectView("getRoleInfList.do"), map);
	}
	
	/**
	 * 获取菜单按钮树
	 * @return
	 */
	@RequestMapping("/getMenuBtnTree.do")
	@ResponseBody
	public List<Map<String, Object>> getMenuBtnTree(HttpServletRequest request, int insLevel, String roleUuid, String roleType){
		return appRoleInfService.getMenuBtnTree(request, insLevel, roleUuid, roleType);
	}

	/**
	 * 保存角色菜单按钮数据
	 * @param request
	 * @param response
	 * @param menuAuth
	 * @return
	 */
	@RequestMapping(value="/saveMenuAuth.do", method=RequestMethod.POST)
	public ModelAndView saveMenuAuth(HttpServletRequest request, HttpServletResponse response, String role_uuid, AppMenuAuth menuAuth){
		Map map = super.getLoginUser(request, response);
		appRoleInfService.saveMenuAuth(request, role_uuid, menuAuth.getMenuAuth(), map);
		return new ModelAndView(new RedirectView("getRoleInfList.do"), map);
	}
	
	/**
	 * 根据角色代码查询出所拥有的权限
	 * @param roleUuid 角色代码
	 * @return
	 */
	@RequestMapping("/getMenuAuthByRoleId.do")
	@ResponseBody
	public String getMenuAuthByRoleId(String roleUuid){
		return appRoleInfService.getMenuAuthByRoleId(roleUuid);
	}
	
	@RequestMapping("/roleNameIsExist.do")
	@ResponseBody
	public boolean roleNameIsExist(HttpServletRequest request, String roleName, String insUuid){
		return appRoleInfService.roleNameIsExist(roleName, insUuid, request);
	}
}
